package cn.damai.mine.other;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;

public class BlockingQueueTest {

	public static void main(String[] args) {

		BlockingQueue<String> q = new PriorityBlockingQueue<String>();

		Producer p = new Producer(q, "abc");
		Consumer c1 = new Consumer(q);
		Consumer c2 = new Consumer(q);
		new Thread(p).start();
		new Thread(c1).start();
		new Thread(c2).start();

	}

}

class Producer implements Runnable {
	
	private final BlockingQueue<String> queue;

	private String sqlStr;

	public Producer(BlockingQueue<String> q, String sqlStr) {
		this.queue = q;
		this.sqlStr = sqlStr;
	}

	public void run() {
		try {
			queue.put(produce());
		} catch (InterruptedException ex) {
			ex.printStackTrace();
		}
	}

	public String produce() {
		return sqlStr;
	}

}

class Consumer implements Runnable {

	private final BlockingQueue<String> queue;

	public Consumer(BlockingQueue<String> q) {
		this.queue = q;
	}

	public void run() {
		try {
			while (true) {
				consume(queue.take());
			}
		} catch (InterruptedException ex) {
			ex.printStackTrace();
		}
	}

	public void consume(String sqlStr) {
		
		// TODO 执行sql操作
		
		System.out.println(sqlStr);
	}
}
